package com.itheima.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class EncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        //1.获取请求路径
        String uri = request.getRequestURI();

        //2.将不需要设置编码的请求直接放行
        if(uri.endsWith(".html")||uri.endsWith(".css")||uri.endsWith(".jsp")){
            chain.doFilter(req, resp);
            return;
        }
        //3.设置编码
        request.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf8");
        //4.放行
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
